#include "socket_programmer.h"
#include <arpa/inet.h>
#include <iostream>
#include <memory.h>
#include <string>
#include <sys/resource.h>
#include <sys/socket.h>
#include <unistd.h>

int advanced_client::tcpserver_bind_and_listen(const std::string addr, int port)
{
    int ret = 0;

    switch (this->get_addr_family()) {
    case AF_INET: {
        sockaddr_in *ipv4_addr = reinterpret_cast<sockaddr_in *>(this->get_server_addr());
        ipv4_addr->sin_family = AF_INET;
        inet_pton(AF_INET, addr.c_str(), &ipv4_addr->sin_addr.s_addr);
        ipv4_addr->sin_port = htons(port);
        break;
    }
    case AF_INET6: {
        sockaddr_in6 *ipv6_addr = reinterpret_cast<sockaddr_in6 *>(this->get_server_addr());
        ipv6_addr->sin6_family = AF_INET6;
        inet_pton(AF_INET6, addr.c_str(), ipv6_addr->sin6_addr.__in6_u.__u6_addr8);
        ipv6_addr->sin6_port = htons(port);
        break;
    }
    default:
        std::cout << "暂时不支持这个地址族: " << this->get_addr_family() << std::endl;
        break;
    }

    ret = bind(this->get_fd(), this->get_server_addr(), this->get_addr_family() == AF_INET ? sizeof(sockaddr_in) : sizeof(sockaddr_in6));
    if (ret == -1) {
        perror("Faid to bind socket fd");
        return ret;
    }
    std::cout << "Bind server socket successfully" << std::endl;

    ret = listen(this->get_fd(), ::max_listen_num);
    if (ret == -1) {
        perror("Faid to listen socket");
        return ret;
    }
    std::cout << "Listen server socket successfully" << std::endl;

    return ret;
}

int advanced_client::tcpserver_accept()
{
    int ret = 0;

    this->cfd = ret = accept(this->get_fd(), client_addr, &client_addr_len);

    if (ret == -1) {
        perror("Fail to accept");
        return ret;
    }

    std::cout << "Accept server socket successfully" << std::endl;

    return ret;
}

int main()
{

    struct rlimit rl;
    rl.rlim_cur = RLIM_INFINITY; // 设置当前限制为无限
    rl.rlim_max = RLIM_INFINITY; // 设置最大限制为无限
    setrlimit(RLIMIT_CORE, &rl); // 设置 Core Dump 文件大小限制

    int recv_msg_len = 0;
    char recv_msg_buf[1024] = {0};
    char send_msg_buf[1024] = {0};

    advanced_client server_socket(AF_INET6);

    if (server_socket.tcpserver_bind_and_listen(std::string("1111::1"), 6666) == -1) {
        return -1;
    }

    if (server_socket.tcpserver_accept() == -1) {
        return -1;
    }

    display_client_info(AF_INET6, server_socket.get_client_addr());
    /* Sleep here for displaying client information */
    sleep(10);

    while (true) {
        bzero(send_msg_buf, sizeof(send_msg_buf));
        bzero(recv_msg_buf, sizeof(recv_msg_buf));

        recv_msg_len = read(server_socket.get_cfd(), recv_msg_buf, sizeof(recv_msg_buf));
        if (recv_msg_len > 0) {
            std::cout << "客户端发来了一条消息：" << recv_msg_buf << std::endl;
            sprintf(send_msg_buf, "我是服务器，我收到了你发来的原消息: %s\n", recv_msg_buf);
            write(server_socket.get_cfd(), send_msg_buf, sizeof(send_msg_buf));
        } else if (recv_msg_len == 0) {
            std::cout << "客户端已经关闭了连接..." << std::endl;
            break;
        } else {
            perror("连接出现了一些错误...");
            break;
        }
    }

    return 0;
}